package world3d.world.entities;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;
import world3d.world.Entity;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLProfile;
import javax.media.opengl.fixedfunc.GLLightingFunc;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
import java.io.IOException;
import java.io.InputStream;

public class SkyAirEntity extends Entity {
	private GLU glu;
	private float radius = 0.378f;
	private int radiusIncVector = 1;
	private Texture solarPanelTexture;

	@Override
	public void display() {
//		final GL2 gl = drawable.getGL().getGL2();
		gl2.glColor3f(0.0f, 0.0f, 1f);
//		gl2.glColorMaterial(GL.GL_FRONT, GLLightingFunc.GL_AMBIENT_AND_DIFFUSE);
//		GLUquadric earth = glu.gluNewQuadric();
//		glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
//		glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
//		glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);

		float[] rgba = {0.0f, 1f, 1f};
		gl2.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
		gl2.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
		gl2.glMaterialf(GL2.GL_FRONT, GL2.GL_SHININESS, 0.5f);

		GLUquadric earth = glu.gluNewQuadric();
		glu.gluQuadricTexture(earth, true);
		glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
		glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
		glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);

		solarPanelTexture.enable(gl);
		solarPanelTexture.bind(gl);

//		glu.gluQuadricTexture(solarPanelTexture, true);
		final int slices = 36;
		final int stacks = 36;
		glu.gluSphere(earth, radius, slices, stacks);
		glu.gluDeleteQuadric(earth);
		gl2.glPushMatrix();
		radius+=.0001* radiusIncVector;
		if(radius>2) {
			radiusIncVector = -1;
		} else if(radius<1.5) {
			radiusIncVector = 1;
		}
		glu.gluQuadricTexture(earth, true);
	}

	@Override
	public void init(GLAutoDrawable glAutoDrawable) {
		super.init(glAutoDrawable);
		try {
			InputStream stream = getClass().getResourceAsStream("/spaceAir.jpg");
			TextureData data = TextureIO.newTextureData(GLProfile.getDefault(), stream, false, "jpg");
			solarPanelTexture = TextureIO.newTexture(data);
		}
		catch (IOException exc) {
			exc.printStackTrace();
			System.exit(2);
		}
		//
		glu = new GLU();
	}

	@Override
	public void reshape(int x, int y, int width, int height) {}

	@Override
	public void dispose() {

	}
}
